<?php

/**
 * JingYao-backend
 *
 * @link     https://gitee.com/wang-zhihui-release/jingyao-backend
 * @apiDocument https://gitee.com/wang-zhihui-release/jingyao-backend/wikis/
 */

namespace App\Enums;

use App\Contacts\EnumInterface;

class Enum implements EnumInterface
{
    public static function toArray(): array
    {
        $rcs = new \ReflectionClass(static::class);
        $consts = $rcs->getConstants();
        $array = [];
        foreach ($consts as $name => $value) {
            if (substr($name, 0, 2) == '__') {
                $array[$consts[substr($name, 2)]] = $value;
            }
        }
        return $array;
    }

    public static function desc($name, $default = null)
    {
        return self::toArray()[$name] ?? $default;
    }

    public static function name($desc, $default = null)
    {
        foreach (self::toArray() as $name => $value) {
            if ($value == $desc) {
                return $name;
            }
        }

        return $default;
    }
}
